<template>
  <el-row class="login-container" type="flex" justify="center" align="middle">
    <el-col :xs="24" :sm="12" :md="8" :lg="6">
      <el-card class="login-card" shadow="hover">
        <template #header>
          <div class="login-header">
            <el-icon size="28" color="#409EFF"><UserFilled /></el-icon>
            <h2 class="login-title">用户登录</h2>
          </div>
        </template>

        <el-form 
          :model="form" 
          :rules="rules" 
          ref="loginForm"
          @submit.prevent="handleLogin"
          status-icon
          label-position="top"
        >
          <el-form-item label="用户名" prop="username">
            <el-input
              v-model="form.username"
              placeholder="请输入用户名"
              prefix-icon="User"
              prefix-icon-style="color: #409EFF"
              clearable
            />
          </el-form-item>

          <el-form-item label="密码" prop="password">
            <el-input
              v-model="form.password"
              type="password"
              placeholder="请输入密码"
              prefix-icon="Lock"
              prefix-icon-style="color: #409EFF"
              show-password
              clearable
            />
          </el-form-item>

          <el-form-item>
            <el-button
              type="primary"
              native-type="submit"
              :loading="isSubmitting"
              class="login-btn"
            >
              {{ isSubmitting ? '登录中...' : '立即登录' }}
            </el-button>
          </el-form-item>

          <el-alert
            v-if="error"
            :title="error"
            type="error"
            :closable="false"
            show-icon
          />
        </el-form>
      </el-card>
    </el-col>
  </el-row>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { UserFilled } from '@element-plus/icons-vue'
import { login, getInfo } from '@/api/Login'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = useRouter()

// 响应式数据
const form = ref({
  username: '',
  password: ''
})
const isSubmitting = ref(false)
const error = ref('')

// 表单验证规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

// 登录处理
const handleLogin = () => {
  isSubmitting.value = true
  error.value = ''

  login(form.value.username, form.value.password)
    .then(res => {
      if (res.data.code === 200) {
        ElMessage.success('登录成功')
        router.push('/home')
      } else {
        error.value = res.data.msg || '登录失败，请检查输入'
        ElMessage.error(res.data.msg)
      }
    })
    .catch(err => {
      error.value = '请求异常，请稍后重试'
      ElMessage.error('网络请求异常')
    })
    .finally(() => {
      isSubmitting.value = false
    })
}

// 自动登录验证
onMounted(() => {
  getInfo().then(res => {
    if (res.data.code === 200) {
      ElMessage.info('已登录，自动跳转中...')
      router.push('/home')
    }
  })
})
</script>

<style scoped>
.login-container {
  height: 100vh;
  background: linear-gradient(120deg, #a1c4fd 0%, #c2e9fb 100%);
}

.login-card {
  border-radius: 12px;
}

.login-header {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.login-title {
  margin: 0;
  font-size: 1.5rem;
  color: #409EFF;
}

.login-btn {
  width: 100%;
  margin-top: 10px;
  letter-spacing: 2px;
}
</style>
